<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_New extends Controller_Admin_Based
{

    public function action_list()
    {

        $pagination_arr = Paginationhelp::get_data('new', 'id_news');

        $category = Category::instances()->get_category();

        $data = array(
            'result' => $pagination_arr['data'],
            'category' => $category,
            'page_view' => $pagination_arr['pagination']
        );


        $view = Viewer::display('admin/new_list', $data);
        $this->response->body($view);
    }

    public function action_add()
    {
        $category = Category::instances()->get_category();
        $data = array(
            'category' => $category,
        );
        $view = Viewer::display('admin/new_add', $data);
        $this->response->body($view);
    }

    public function action_do_add()
    {

        if ($_POST)
        {
            if ($_POST['addtime'] == '')
            {
                $_POST['addtime'] = time();
            }
            else
            {
                $_POST['addtime'] = Time::instance()->get_mk_time($_POST['addtime']);
            }
            $_POST['updatetime'] = time();



            $images = Validation::factory($_FILES);
            if ($images->check())
            {
                $images_path = Uploads::handleupload('/uploads/images', $_FILES['images']);
            }
            $_POST['images'] = $images_path;
            $new = News::instances()->set($_POST);
            if ($new)
            {
                Message::set('文章添加成功！');
                Request::current()->redirect('/admin/new/add');
            }
        }
    }

    public function action_edit()
    {
        $id_news = $this->request->param('id');

        $new = News::instances($id_news)->get();
        $category = Category::instances()->get_category();
        $view = Viewer::display('admin/new_edit', array('data' => $new, 'category' => $category));
        $this->response->body($view);
    }

    public function action_do_edit()
    {
        $id_news = $this->request->param('id');
        $news = News::instances($id_news)->get();
        if ($_POST)
        {
            if ($_POST['addtime'] == '')
            {
                $_POST['addtime'] = time();
            }
            else
            {
                $_POST['addtime'] = Time::instance()->get_mk_time($_POST['addtime']);
            }
            $_POST['updatetime'] = time();

            if ($_FILES['images']['tmp_name'] != '')
            {
                $images = Validation::factory($_FILES);
                if ($images->check())
                {
                    $images_path = Uploads::handleupload('/uploads/images', $_FILES['images']);
                }
            } else {
                 $_POST['images'] = $news['images'];
            }

            $new = News::instances($id_news)->update($_POST);
            if ($new)
            {
                Message::set('文章修改成功！');
                Request::current()->redirect('/admin/new/edit/' . $id_news);
            }
        }
    }

    public function action_delete()
    {
        $id_news = $this->request->param('id');
        $flag = News::instances($id_news)->delete();
        if ($flag)
        {
            Message::set('新闻删除成功');
            Request::current()->redirect('/admin/new/list');
        }
        else
        {
            Message::set('新闻删除失败', 'error');
            Request::current()->redirect('/admin/new/list');
        }
    }

}